package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.service.INursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> list = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlanVo);
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        this.save(nursingPlan);
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (!org.springframework.util.CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }
        boolean result = nursingProjectPlanService.saveBatch(projectPlans);
        return result ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(!CollectionUtils.isEmpty(projectPlans)){
            nursingProjectPlanService.remove((Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId , nursingPlanDto.getId())));
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(nursingPlanDto.getId());
        });
        nursingProjectPlanService.saveBatch(projectPlans);
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean result = updateById(nursingPlan);
        return result ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> getAll() {
        List<NursingPlan> list = list(Wrappers.lambdaQuery());
        return list;
    }
}
